<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .register-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 350px;
        }

        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 25px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }

        input[type="text"],
        input[type="password"],
        input[type="email"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .error-message {
            color: red;
            margin-top: 5px;
            font-size: 14px;
        }

        .success-message {
            color: green;
            margin-top: 10px;
            text-align: center;
            font-size: 14px;
        }
    </style>

</head>
<body>
<div class="register-container">
    <h2>用户注册</h2>
    <form id="registerForm">
        <div class="form-group">
            <label for="userName">用户名</label>
            <input type="text" id="userName" name="userName" required placeholder="请输入用户名">
            <div id="userNameError" class="error-message"></div>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required placeholder="请输入密码">
            <div id="passwordError" class="error-message"></div>
        </div>

        <div class="form-group">
            <label for="idCard">身份证号</label>
            <input type="text" id="idCard" name="idCard" required placeholder="请输入身份证号">
            <div id="idCardError" class="error-message"></div>
        </div>

        <div class="form-group">
            <label for="email">邮箱</label>
            <input type="email" id="email" name="email" required placeholder="请输入邮箱">
            <div id="emailError" class="error-message"></div>
        </div>

        <div class="form-group">
            <label for="phone">联系电话</label>
            <input type="text" id="phone" name="phone" required placeholder="请输入联系电话">
            <div id="phoneError" class="error-message"></div>
        </div>

        <button type="submit">注册</button>
        <div id="successMessage" class="success-message"></div>
    </form>
</div>
<script src="./js/jquery.min.3.6.0.js"></script>
<script>
    $(document).ready(function() {
        $('#registerForm').submit(function(e) {
            e.preventDefault();

            // 清除之前的错误和成功消息
            $('.error-message').text('');
            $('#successMessage').text('');

            // 获取表单数据
            const userName = $('#userName').val().trim();
            const password = $('#password').val().trim();
            const idCard = $('#idCard').val().trim();
            const email = $('#email').val().trim();
            const phone = $('#phone').val().trim();

            // 简单验证
            let isValid = true;

            if (userName === '') {
                $('#userNameError').text('用户名不能为空');
                isValid = false;
            }

            if (password === '') {
                $('#passwordError').text('密码不能为空');
                isValid = false;
            } else if (password.length < 6) {
                $('#passwordError').text('密码长度不能少于6位');
                isValid = false;
            }

            if (idCard === '') {
                $('#idCardError').text('身份证号不能为空');
                isValid = false;
            }

            if (email === '') {
                $('#emailError').text('邮箱不能为空');
                isValid = false;
            }

            if (phone === '') {
                $('#phoneError').text('联系电话不能为空');
                isValid = false;
            }

            if (!isValid) {
                return;
            }

            // 准备发送的数据
            const userData = {
                userName: userName,
                password: password,
                idCard: idCard,
                email: email,
                phone: phone
            };

            // AJAX调用后台接口
            $.ajax({
                url: '/sysUser/save',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(userData),
                success: function(response) {
                    if (response.success) {
                        $('#successMessage').text('注册成功！');
                        $('#registerForm')[0].reset()
                        window.location.href = './index.html';

                    } else {
                        if (response.message) {
                            $('#userNameError').text(response.message);
                        } else {
                            $('#userNameError').text('注册失败，请稍后重试');
                        }
                    }
                },
                error: function(xhr, status, error) {
                    let errorMsg = '注册过程中发生错误';
                    if (xhr.responseJSON && xhr.responseJSON.message) {
                        errorMsg = xhr.responseJSON.message;
                    }
                    $('#userNameError').text(errorMsg);
                }
            });
        });
    });
</script>
</body>
</html>
